package com.renchuang.rfidtest.activity

import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.media.AudioManager
import android.media.ToneGenerator
import android.nfc.NfcAdapter
import android.os.Bundle
import androidx.activity.ComponentActivity
import com.renchuang.rfidtest.BuildConfig
import com.renchuang.rfidtest.databinding.ActivityMainBinding
import com.renchuang.rfidtest.utils.DateTimeHelper
import com.renchuang.rfidtest.utils.NFCHelper
import com.ocm.bracelet_machine_sdk.BraceletMachineManager

class MainActivity : ComponentActivity() {
    private var binding: ActivityMainBinding? = null
    private var mPendingIntent: PendingIntent? = null
    private var receiver: BroadcastReceiver? = null
    private var toneGen = ToneGenerator(AudioManager.STREAM_MUSIC, 100)
    private var mAdapter: NfcAdapter? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater).also {  activityBinding ->
            setContentView(activityBinding.root)
            val type = if(BraceletMachineManager.isIC()) "IC" else "ID"
            activityBinding.tvVersion.text = "v${BuildConfig.VERSION_NAME}_${type}"//resources.getString(R.string.app_version, "${}${if (EquipmentManager.isDemo) ".dev" else ""}")
            activityBinding.tvDateTime.text = DateTimeHelper.getDateTime()
        }
        setupReceiver()
        try {
            NFCHelper.closeZHRFID()
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        openNFC()
    }

    override fun onResume() {
        super.onResume()
        mAdapter?.enableForegroundDispatch(this, mPendingIntent, null, null)
    }


    public override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        NFCHelper.decode(intent)
    }

    private fun openNFC() {
        mAdapter = NfcAdapter.getDefaultAdapter(this)
        mPendingIntent = PendingIntent.getActivity(
            this, 0,
            Intent(this, javaClass).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
            PendingIntent.FLAG_IMMUTABLE
        )
        val runnable = Runnable {
                binding?.tvRead?.text = ""
                binding?.tvReadBlock?.text = ""
        }
        NFCHelper.listener = object : NFCHelper.NFCListener {
            override fun onNFCReadSuccess(cardNo10D: String, blockContent: String?) {
                binding?.tvRead?.removeCallbacks(runnable)
                binding?.tvRead?.text = cardNo10D
                binding?.tvReadBlock?.text = blockContent
                toneGen.startTone(ToneGenerator.TONE_PROP_BEEP, 300)
                binding?.tvRead?.postDelayed(runnable, 3000)
            }
        }
        NFCHelper.openZHRFID()
    }


    //监听广播
    private fun setupReceiver() {
        receiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                intent?.let {
                    when(it.action) {
                        Intent.ACTION_TIME_TICK -> {
                            binding?.tvDateTime?.text = DateTimeHelper.getDateTime()
                        }
                        else -> {}
                    }
                }
            }
        }
        val intentFilter = IntentFilter()
        intentFilter.addAction(Intent.ACTION_TIME_TICK)
        registerReceiver(receiver, intentFilter)
    }

    override fun onDestroy() {
        unregisterReceiver(receiver)
        super.onDestroy()
    }
}